<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Article_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $good_id
     * @return mixed
     * 查询单条
     */
    public function get_all_articles($limit,$start,$cate = 0){

        $this->db->select('*');
        $this->db->from('articles');
        $this->db->limit($start,$limit);
        $this->db->order_by('order','asc');
        if(!empty($cate)){
            $this->db->where('article_category',$cate);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_all_articles_count($cate = 0){

        $this->db->select('*');
        $this->db->from('articles');
        $this->db->order_by('order','asc');
        if(!empty($cate)){
            $this->db->where('article_category',$cate);
        }
        $total=$this->db->count_all_results();
        return $total;
    }

    //添加资讯
    public function add_articles($data){
        $this->db->insert('articles',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    // 修改资讯
    
    public function sava_article($article_id,$data){
        $this->db->where('article_id',$article_id);
        $this->db->update('articles', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_articles_by_id($article_id){
        $this->db->select('*');
        $this->db->from('articles');
        $this->db->where('article_id',$article_id);
        $query = $this->db->get();
        return $query->row_array();
    }
    //查找是否收藏了该文章
    public function get_favorite_by_user_id($user_id,$item_id){
        $this->db->select('*');
        $this->db->from('user_favorites');
        $this->db->where(array('user_id'=>$user_id,'item_id'=>$item_id,'item_type'=>2));
        $query = $this->db->get();
        return $query->row_array();
    }
    //查找前5条按照被推荐的文章  按照时间先后排序
    public function get_five_articles($id=0){
        $this->db->select('*')->from('articles')->where('article_god',1)->where('article_id <>',$id)->limit(5)->order_by('article_id','desc');
        $query = $this->db->get();
        return $query->result_array();
    }
    //删除文章
    public function del_article($id){
        $this->db->where('article_id',$id)->delete('articles');
    }
    //搜索关键字资讯的总数
    public function count_article_find($key=array()){
        $this->db->select('article_id');
        $this->db->from('articles');
        if($key){
            $this->db->group_start();
            foreach($key as $k => $v){
                $this->db->or_like('article_title', $v);
            }
            $this->db->group_end();
        }
        // $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total; 
    }

    //搜索文章的关键字
    public function article_find($start, $limit, $key=array(),$order){
        $this->db->select('*');
        $this->db->from('articles');
        if($key){
            $this->db->group_start();
            foreach($key as $k => $v){
                $this->db->or_like('article_title', $v);
            }
            $this->db->group_end();
        }       
        // $this->db->where('good_lock',0);
        $this->db->limit($limit,$start);
        $this->db->order_by('article_add_time',$order);
        
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_articles_by_cate_id($cate_id){
        $this->db->select('article_title,article_content');
        $this->db->from('articles');
        $this->db->where('article_category',$cate_id);
        $this->db->order_by('article_add_time',"desc");
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_articles_list_by_cate_id($cate_id){
        $this->db->select('*');
        $this->db->from('articles');
        $this->db->where('article_category',$cate_id);
        $this->db->order_by('order',"asc");
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_news_by_id($id){
        $this->db->select('article_id,article_title,article_content,article_add_time,article_category,article_cover,article_memo');
        $this->db->from('articles');
        $this->db->where('article_id',$id);
        $this->db->order_by('article_add_time',"desc");
        $query = $this->db->get();
        return $query->row_array();
    }



}
